package com.wie.controller;

import com.wie.constant.MessageConstant;
import com.wie.domain.dto.ChinaGdpListDTO;
import com.wie.domain.dto.CountryGdpListDTO;
import com.wie.domain.dto.ProvinceGdpListDTO;
import com.wie.domain.entity.CountryGdp;
import com.wie.domain.entity.GdpData;
import com.wie.domain.entity.ProvinceGdp;
import com.wie.domain.entity.Result;
import com.wie.domain.vo.*;
import com.wie.service.CountryGdpService;
import com.wie.service.GdpDataService;
import com.wie.service.ProvinceGdpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/queryData")
@CrossOrigin(allowCredentials = "true", allowedHeaders = "true")
public class QueryDataController {

    @Autowired
    private GdpDataService gdpDataService;
    @Autowired
    private ProvinceGdpService provinceGdpService;
    @Autowired
    private CountryGdpService countryGdpService;

    /**
     * 查询中国一段时间内的GDP
     * @param chinaGdpListDTO
     * @return
     */
    @PostMapping("/chinaGdpList")
    public Result<ChinaGdpListVO> chinaGdpList(@RequestBody ChinaGdpListDTO chinaGdpListDTO) {
        log.info("查询中国GDP, {}", chinaGdpListDTO);
        List<GdpData> gdpDataList = gdpDataService.lambdaQuery()
                .ge(chinaGdpListDTO.getBeginYear() != null && chinaGdpListDTO.getBeginYear() != "", GdpData::getYear, chinaGdpListDTO.getBeginYear())
                .le(chinaGdpListDTO.getEndYear() != null && chinaGdpListDTO.getEndYear() != "", GdpData::getYear, chinaGdpListDTO.getEndYear())
                .orderByDesc(GdpData::getYear)
                .list();
        List<GdpDataVO> chinaGdpListVO = new ArrayList<>();
        for(GdpData gdpData: gdpDataList) {
            GdpDataVO gdpDataVO = new GdpDataVO().builder()
                    .year(gdpData.getYear())
                    .gdp(gdpData.getGdp())
                    .build();
            chinaGdpListVO.add(gdpDataVO);
        }
        return Result.success(chinaGdpListVO);
    }

    /**
     * 查询中国某个省份一段时间内的GDP
     * @param provinceGdpListDTO
     * @return
     */
    @PostMapping("/provinceGdpList")
    public Result<ProvinceGdpListVO> provinceGdpList(@RequestBody ProvinceGdpListDTO provinceGdpListDTO) {
        log.info("查询中国某省一段时间内的GDP, {}", provinceGdpListDTO);
        List<ProvinceGdp> provinceGdpList = provinceGdpService.lambdaQuery()
                .eq(provinceGdpListDTO.getProvince() != null && provinceGdpListDTO.getProvince() != "", ProvinceGdp::getProvince, provinceGdpListDTO.getProvince())
                .ge(provinceGdpListDTO.getBeginYear() != null && provinceGdpListDTO.getBeginYear() != "", ProvinceGdp::getYear, provinceGdpListDTO.getBeginYear())
                .le(provinceGdpListDTO.getEndYear() != null && provinceGdpListDTO.getEndYear() != "", ProvinceGdp::getYear, provinceGdpListDTO.getEndYear())
                .orderByDesc(ProvinceGdp::getYear)
                .list();
        List<ProvinceGdpVO> provinceGdpListVO = new ArrayList<>();
        for(ProvinceGdp provinceGdp: provinceGdpList) {
            ProvinceGdpVO ProvinceGdpVO = new ProvinceGdpVO().builder()
                    .province(provinceGdp.getProvince())
                    .year(provinceGdp.getYear())
                    .gdp(provinceGdp.getGdp())
                    .build();
            provinceGdpListVO.add(ProvinceGdpVO);
        }
        return Result.success(provinceGdpListVO);
    }

    /**
     * 查询某个国家一段时间内的GDP
     * @param countryGdpListDTO
     * @return
     */
    @PostMapping("/countryGdpList")
    public Result<CountryGdpListVO> countryGdpList(@RequestBody CountryGdpListDTO countryGdpListDTO) {
        log.info("查询某个国家一段时间内的GDP, {}", countryGdpListDTO);
        List<CountryGdp> countyrGdpList = countryGdpService.lambdaQuery()
                .eq(countryGdpListDTO.getCountry() != null && countryGdpListDTO.getCountry() != "", CountryGdp::getCountry, countryGdpListDTO.getCountry())
                .ge(countryGdpListDTO.getBeginYear() != null && countryGdpListDTO.getBeginYear() != "", CountryGdp::getYear, countryGdpListDTO.getBeginYear())
                .le(countryGdpListDTO.getEndYear() != null && countryGdpListDTO.getEndYear() != "",CountryGdp::getYear, countryGdpListDTO.getEndYear())
                .orderByDesc(CountryGdp::getYear)
                .list();
        List<CountryGdpVO> countryGdpListVO = new ArrayList<>();
        for (CountryGdp countryGdp : countyrGdpList) {
            CountryGdpVO countryGdpVO = new CountryGdpVO().builder()
                    .country(countryGdp.getCountry())
                    .year(countryGdp.getYear())
                    .gdp(countryGdp.getGdp())
                    .build();
            countryGdpListVO.add(countryGdpVO);
        }
        return Result.success(countryGdpListVO);
    }
}
